<template>
  <div class="app-container">
    <!--工具栏-->
    <el-dialog
      title="请选择打印模板"
      :visible.sync="crud.printConfig.pringDialog"
      width="30%"
    >
      <jPrintConfig groupId="financeDailyCost"
                    v-if="crud.printConfig.pringDialog"
                    @getPrintId="crud.toPrint"
                    :isPreview="crud.printConfig.isPreview"
      ></jPrintConfig>
    </el-dialog>
    <div class="head-container">
      <crudOperation :permission="permission">
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          @click="crud.toDetail('financeDailyCostDetail')"
        >
          新增
        </el-button>
        <el-button slot="left" class="filter-item" size="mini" type="warning" :disabled="crud.selections.length !== 1"
                   :loading="crud.copyLoading" @click="copyOrder(crud.selections)" v-permission="permission.add">
          复制
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="danger"
          :loading="crud.rejectLoading"
          icon="el-icon-remove-outline"
          @click="audit('PASS','审核')"
          :disabled="crud.selections.length < 1"
          v-permission="permission.pass"
        >
          审核
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          :loading="crud.rejectLoading"
          type="success"
          icon="el-icon-circle-check"
          :disabled="crud.selections.length < 1"
          @click="audit('REJECT','反审')"
          v-permission="permission.reject"
        >
          反审
        </el-button>
<!--        <el-button-->
<!--          slot="right"-->
<!--          class="filter-item"-->
<!--          size="mini"-->
<!--          type="primary"-->
<!--          @click="crud.toPrint()"-->
<!--        >-->
<!--          打印-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          slot="right"-->
<!--          class="filter-item"-->
<!--          size="mini"-->
<!--          type="primary"-->
<!--          @click="crud.toPrint()"-->
<!--        >-->
<!--          打印预览-->
<!--        </el-button>-->
      </crudOperation>
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="70px" v-formkey="{query:crud.toQuery}">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="业务日期">
                <div class="flex flex-row">
                  <el-date-picker
                    v-model="query.startTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="开始日期"
                  />
                  <el-date-picker
                    v-model="query.endTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="结束日期"
                  />
                </div>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="单据编号">
                <el-input v-model="query.code" placeholder="请输入单据编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="供应商">
                <zSelect :value.sync="query.supplierId" url="api/dropDown/basCustomerList" style="width:  100%"
                         :params="{ isContact: '1,2' }"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户">
                <zSelect :value.sync="query.customerId" url="api/dropDown/basCustomerList"
                         :params="{ isContact: '0,2' }"
                         style="width: 100%;"/>
              </el-form-item>
            </el-col>
          </el-row>
            <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="状态">
                <el-select v-model="query.orderStatus" class="filter-item" clearable placeholder="请选择状态" size="small"
                           style="width: 100%;">
                  <el-option
                    v-for="item in dict.common_order_status"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="所属部门">
                <treeselect
                  v-model="query.deptId"
                  :options="depts"
                  placeholder="选择部门"
                  :normalizer="normalizer"
                >
                </treeselect>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="经办人">
                <zSelect :value.sync="query.userId" url="api/dropDown/sysUserList" :params="{deptId:query.deptId}"
                         style="width:  100%"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="银行">
                <zSelect :value.sync="query.bankId" url="api/dropDown/basBankList"/>
              </el-form-item>
            </el-col>
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item label="费用类型">
                  <el-select v-model="query.type" class="filter-item" clearable placeholder="请选择费用类型" size="small"
                             style="width: 100%;">
                    <el-option
                      v-for="item in dict.t_finance_daily_cost_type"
                      :key="item.id"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="审核时间">
                <el-date-picker
                  v-model="query.auditTime"
                  type="daterange"
                  @change="getValue()"
                  align="right"
                  unlink-panels
                  style="width: 100%"
                  format="yyyy - MM - dd "
                  value-format="yyyy-MM-dd"
                  default-value
                  range-separator=""
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="打印状态">
                <el-select v-model="query.isPrint" class="filter-item" clearable placeholder="状态" size="small"
                           style="width: 100%;">
                  <el-option v-for="item in dict.common_yes_or_no" :key="item.id" :label="item.label"
                             :value="item.value"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="备注">
                <el-input v-model="query.remark" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" align="center">
              <rrOperation :crud="crud"/>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <el-dialog title="表格配置"
                 :visible.sync="tableConfigShow"
                 width="60%">
        <j-table-column-config :columns.sync="currentHeader"  @tabSave="saveHead"/>
      </el-dialog>
      <right-click-menu @tableConfig="tableConfig" :options.sync="rightClickMenuConfig"></right-click-menu>
      <!--表格渲染-->
      <el-table ref="table"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                @sort-change="sortTab"
                @header-dragend="changeColWidth"
                v-draghead="manyTableOption"
                :row-class-name="crud.tableRowClassName"
                border v-loading="crud.loading" :data="crud.data" size="small" height=700 style="width: 100%;"
                @row-dblclick="crud.toDetailPlus('financeDailyCostDetail',$event.id)"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
        <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
        <el-table-column v-if="item.visible"
                         :sortable="item.sortable?'custom':false"
                         :sort-orders="['descending', 'ascending', null]"
                         v-for="(item, index) in header" :key="`col_${index}`" :prop="item.prop" :label="columnLabel(item)" :width="item.width + 'px'" @header-dragend="changeColWidth" ><template slot-scope='{row,$index}'><span v-if='item.type == 1'>{{ row[`${item.prop}`] }}</span><span v-else-if='item.type == 2' >{{ parseTime(row[`${item.prop}`])}}</span><span v-else-if='item.type == 3' >{{ getDict(`this.dict.label.${item.dictionary}[${row[item.prop]}]`) }}</span><span v-else-if="item.type == 4"><span v-if="item.prop == 'auditTime'"> <span>{{ parseTime(row.auditTime) }}</span> </span><span v-if="item.prop == 'orderStatus'"> {{ dict.label.common_order_status[row.orderStatus] }} </span><span v-if="item.prop == 'isPrint'"> {{ dict.label.common_yes_or_no[row.isPrint] }} </span></span> </template> </el-table-column> <el-table-column :render-header="crud.renderHeader" v-permission="['admin','financeDailyCost:edit','financeDailyCost:del']" label="操作" width="150px" align="center"> <template slot-scope="scope"> <udOperation :data="scope.row" :permission="permission" /> </template> </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudFinanceDailyCost from '@/api/erp/financeDailyCost'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from "@riophae/vue-treeselect"
import {getDepts} from "@/api/system/dept";
import jPrintConfig from '@/components/jPrintConfig/jPrintConfig'
import{manyTableMixin}from"../../../mixins/manyTableMixin"
// crud交由presenter持有
const defaultCrud = CRUD({
  title: '日常费用',
  url: 'api/financeDailyCost',
  sort: 'id,desc',
  query: {
    busDate: null,
    orderCode: null,
    supplierId: null,
    customerId: null,
    orderStatus: null,
    deptId: null,
    userId: null,
    bankId: null,
    auditTime: null,
    isPrint: null
  },
  optShow: {
    add: false,
    edit: false,
    del: true,
    download: true,
    downloadDetail: true,
    leadingIn: true,
    enable: false,
    disable: false,
    pass: false,
    reject: false,
    print: true,
    printDetail: true
  },
  crudMethod: {...crudFinanceDailyCost}
})
const defaultForm = {
  id: null,
  code: null,
  name: null,
  type: null,
  busDate: null,
  supplierId: null,
  supplierCode: null,
  supplierName: null,
  customerId: null,
  customerCode: null,
  customerName: null,
  deptId: null,
  deptCode: null,
  deptName: null,
  userId: null,
  userCode: null,
  userName: null,
  bankId: null,
  bankCode: null,
  bankName: null,
  bankAccount: null,
  bankOpenName: null,
  bankOpenBranch: null,
  taxAmount: null,
  isPrint: null,
  printCount: null,
  seq: null,
  orderStatus: null,
  remark: null,
  orderRemark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null,
  auditUserId: null,
  auditUserCode: null,
  auditUserName: null,
  auditTime: null,
  closeUserId: null,
  closeUserCode: null,
  closeUserName: null,
  closeTime: null,
  bankList:[]
}
export default {
  name: 'financeDailyCost',
  components: {pagination, crudOperation, rrOperation, udOperation, Treeselect, jPrintConfig},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),manyTableMixin],
  dicts: ['t_finance_bank_in_out_use_type', 'common_yes_or_no', 'common_order_status','t_finance_daily_cost_type'],
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      permission: {
        add: ['admin', 'financeDailyCost:add'],
        edit: ['admin', 'financeDailyCost:edit'],
        del: ['admin', 'financeDailyCost:del'],
        import: ['admin', 'financeDailyCost:import'],
        export: ['admin', 'financeDailyCost:export'],
        copy: ['admin', 'financeDailyCost:copy'],
        upload: ['admin', 'financeDailyCost:upload'],
        download: ['admin', 'financeDailyCost:download'],
        pass: ['admin', 'financeDailyCost:pass'],
        reject: ['admin', 'financeDailyCost:reject'],
        closed: ['admin', 'financeDailyCost:closed'],
        invalid: ['admin', 'financeDailyCost:invalid'],
        stop: ['admin', 'financeDailyCost:stop'],
        recover: ['admin', 'financeDailyCost:recover'],
      },
      depts: [],
      rules: {}
    }
  },
  created() {
    this.getDepts()
  },
  watch: {},
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    getValue(e) {
      this.query.startTime = this.query.dataExtent[0]
      this.query.endTime = this.query.dataExtent[1]
    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    getDepts() {
      getDepts({status: 1}).then(res => {
        this.depts = res
      })
    },
    audit(type,msg) {
      this.crud.rejectLoading = true
      const ids = this.crud.selections.map(m => m.id)
      const data = {
        ids: ids,
        type: type
      }
      crudFinanceDailyCost.passOrReject(data).then(res => {
        if (200 === res.status) {
          this.crud.refresh()
          this.$message.success(msg+"成功")
        }
        this.crud.rejectLoading = false
      }).catch(e => {
        this.crud.rejectLoading = false
      })
    },
    copyOrder(e) {
      const { id } = e[0]
      this.crud.toCopy(id,"/finance/financeDailyCostDetail")
    }
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
